package com.xyxeshang.es.music.backend.util;

import com.alibaba.fastjson.JSON;
import com.xyxeshang.es.music.backend.entity.Song;
import com.xyxeshang.es.music.backend.entity.SongComment;
import com.xyxeshang.es.music.backend.entity.SongLike;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xyx-Eshang
 */
@Component
public class EsRedisUtil {
	@Resource
	private RedisUtil tmpRedisUtil;

	private static RedisUtil redisUtil;

	@PostConstruct
	public void beforeInit() {
		redisUtil = tmpRedisUtil;
	}

	/**
	 * 更新热歌榜
	 *
	 * @param songList
	 */
	public static void setHotBillboard(List<Song> songList) {
		for (Song song : songList) {
			String jsonResult = JSON.toJSONString(song);
			redisUtil.lSet("billboard:hot", jsonResult);
		}
	}

	/**
	 * 获取热歌榜
	 *
	 * @return
	 */
	public static List<Song> getHotBillboard(Integer count) {
		List<Object> objectList = redisUtil.lGet("billboard:hot", 0L, (long) count);
		// 解析成List<Song>
		List<Song> songList = new ArrayList<>();
		for (Object obj : objectList) {
			String json = (String) obj;
			Song song = JSON.parseObject(json, Song.class);
//
//			String songId = song.getSongId();
//			// 2. 从redis中取得评论
//			List<SongComment> songCommentList = getHotComment(songId);
//			Integer songCommentCount = songCommentList.size();
//			// 3. 从redis中取得点赞
//			List<SongLike> songLikeList = getHotLike(songId);
//			Integer songLikeCount = songLikeList.size();
//			// 4. 封装
//			song.setSongCommentList(songCommentList)
//					.setSongCommentCount(songCommentCount)
//					.setSongLikeCount(songLikeCount);

			songList.add(song);
		}
		return songList;
	}

	/**
	 * 清空热歌榜
	 */
	public static void clearHotBillboard() {
		redisUtil.del("billboard:hot");
	}

	/**
	 * 更新新歌榜
	 *
	 * @param songList
	 */
	public static void setNewBillboard(List<Song> songList) {
		for (Song song : songList) {
			String jsonResult = JSON.toJSONString(song);
			redisUtil.lSet("billboard:new", jsonResult);
		}
	}

	/**
	 * 获取新歌榜
	 *
	 * @return
	 */
	public static List<Song> getNewBillboard(Integer count) {
		List<Object> objectList = redisUtil.lGet("billboard:new", 0L, (long) count);
		// 解析成List<Song>
		List<Song> songList = new ArrayList<>();
		for (Object object : objectList) {
			String json = (String) object;
			Song song = JSON.parseObject(json, Song.class);
			songList.add(song);
		}
		return songList;
	}

	/**
	 * 清空新歌榜
	 */
	public static void clearNewBillboard() {
		redisUtil.del("billboard:new");
	}

	/**
	 * 设置飙升榜
	 *
	 * @param songList
	 */
	public static void setUpBillboard(List<Song> songList) {
		for (Song song : songList) {
			String jsonResult = JSON.toJSONString(song);
			redisUtil.lSet("billboard:up", jsonResult);
		}

	}


	/**
	 * 获取飙升榜
	 *
	 * @return
	 */
	public static List<Song> getUpBillboard(Integer count) {
		List<Object> objectList = redisUtil.lGet("billboard:up", 0L, (long) count);
		// 解析成List<Song>
		List<Song> songList = new ArrayList<>();
		for (Object object : objectList) {
			String json = (String) object;
			Song song = JSON.parseObject(json, Song.class);
			songList.add(song);
		}
		return songList;
	}

	/**
	 * 清空飙升榜
	 */
	public static void clearUpBillboard() {
		redisUtil.del("billboard:up");
	}

	/**
	 * 设置抖音排行榜
	 *
	 * @param songList
	 */
	public static void setTikTokBillboard(List<Song> songList) {
		for (Song song : songList) {
			String jsonResult = JSON.toJSONString(song);
			redisUtil.lSet("billboard:tikTok", jsonResult);
		}
	}

	/**
	 * 获取抖音排行榜
	 *
	 * @return
	 */
	public static List<Song> getTikTokBillboard(Integer count) {
		List<Object> objectList = redisUtil.lGet("billboard:tikTok", 0L, (long) count);
		// 解析成List<Song>
		List<Song> songList = new ArrayList<>();
		for (Object object : objectList) {
			String json = (String) object;
			Song song = JSON.parseObject(json, Song.class);
			songList.add(song);
		}
		return songList;
	}

	/**
	 * 清空抖音排行榜
	 */
	public static void clearTikTokBillboard() {
		redisUtil.del("billboard:tikTok");
	}

	/**
	 * 将某个关键词设置为热门搜索词（设置后，将"以该关键词搜索后的数据"存入redis）
	 *
	 * @param key
	 */
	public static List<Song> setHotSearch(String key) throws IOException {
		List<Song> songList = EsMusicUtil.listByKey(key, 0);
		for (Song song : songList) {
			String jsonResult = JSON.toJSONString(song);
			redisUtil.lSet("hotSearch:" + key, jsonResult);
		}
		return songList;
	}

	/**
	 * 获取热门搜索的数据（指的是以这个key作为关键词搜索时，展现出的数据）
	 *
	 * @param key
	 * @return
	 */
	public static List<Song> getHotSearch(String key) {
		List<Object> objectList = redisUtil.lGet("hotSearch:" + key, 0L, -1L);
		//解析成List<Song>
		List<Song> songList = new ArrayList<>();
		for (Object object : objectList) {
			String json = (String) object;
			Song song = JSON.parseObject(json, Song.class);
			songList.add(song);
		}
		return songList;
	}

	/**
	 * 根据热搜词删除热搜数据
	 *
	 * @param key
	 */
	public static void clearHotSearch(String key) {
		redisUtil.del("hotSearch:" + key);
	}

//	/**
//	 * 添加热门歌曲评论
//	 *
//	 * @param songComment
//	 */
//	public static void addHotComment(SongComment songComment) {
//		String songId = songComment.getSongId();
//		String jsonResult = JSON.toJSONString(songComment);
//		redisUtil.lSet("hotComment:" + songId, jsonResult);
//	}
//
//	/**
//	 * 获取热门歌曲的评论
//	 *
//	 * @param songId
//	 * @return
//	 */
//	public static List<SongComment> getHotComment(String songId) {
//		List<Object> objectList = redisUtil.lGet("hotComment:" + songId, 0L, -1L);
//		List<SongComment> songCommentList = new ArrayList<>();
//		for (Object obj : objectList) {
//			String json = (String) obj;
//			SongComment songComment = JSON.parseObject(json, SongComment.class);
//			songCommentList.add(songComment);
//		}
//		return songCommentList;
//	}
//
//	/**
//	 * 根据歌曲id，清空热门歌曲的评论数据
//	 *
//	 * @param songId
//	 */
//	public static void clearHotComment(String songId) {
//		redisUtil.del("hotComment:" + songId);
//	}
//
//	/**
//	 * 添加热门歌曲的点赞
//	 *
//	 * @param songLike
//	 */
//	public static void addHotLike(SongLike songLike) {
//		String songId = songLike.getSongId();
//		String jsonResult = JSON.toJSONString(songLike);
//		redisUtil.lSet("hotLike:" + songId, jsonResult);
//	}
//
//	/**
//	 * 获取热门歌曲的点赞
//	 *
//	 * @param songId
//	 * @return
//	 */
//	public static List<SongLike> getHotLike(String songId) {
//		List<Object> objectList = redisUtil.lGet("hotLike:" + songId, 0L, -1L);
//		List<SongLike> songLikeList = new ArrayList<>();
//		for (Object obj : objectList) {
//			String json = (String) obj;
//			SongLike songLike = JSON.parseObject(json, SongLike.class);
//			songLikeList.add(songLike);
//		}
//		return songLikeList;
//	}
//
//	/**
//	 * 根据歌曲id，清空热门歌曲的点赞数据
//	 *
//	 * @param songId
//	 */
//	public static void clearHotLike(String songId) {
//		redisUtil.del("hotLike:" + songId);
//	}

}
